package com.spark.mooc.ch5_rdd.part02_keyValueRDD

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

/**
 * @description: 图书平均销量
 * @time: 2020/11/27 14:25
 * @author: lhy
 */
object AvgBook {
    def main(args: Array[String]): Unit = {
        val conf: SparkConf = new SparkConf().setAppName("avg of book").setMaster("local")
        val sc = new SparkContext(conf)
        val data: Array[(String, Int)] = Array(("hadoop",5),("com/spark",2),("hadoop",4),("com/spark",9))
        val rdd: RDD[(String, Int)] = sc.parallelize(data)
        val argBook: Array[(String, Int)] = rdd.mapValues(x => (x, 1)).reduceByKey((x, y) => (x._1 + y._1, x._2 + y._2))
          .mapValues(x => (x._1 / x._2)).collect()
        argBook foreach(println)
    }
}
